//
//  EHTitleRow.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/8/23.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import Eureka

class EHTitleRowConfiguration {
    var title: String?
    var image: UIImage?
    var action: (() -> Void)?

    func setTitle(_ title: String?) -> Self { self.title = title; return self }
    func setImage(_ image: UIImage?) -> Self { self.image = image; return self }
    func setAction(_ action: @escaping () -> Void) -> Self { self.action = action; return self }
}

class EHTitleCell: Cell<Bool>, CellType {
    var thumbnail: UIImageView!
    var titleLabel: UILabel!
    var arrowImageView: UIImageView!

    override func setup() {
        super.setup()
        guard let config = (row as? EHTitleRow)?.configuration else { return }

        setupThumbnail()
        setupTitleLabel()
        setupArrowButton()
        configure(config)
        height = { 70 }
    }

    private func configure(_ config: EHTitleRowConfiguration) {
        thumbnail.image = config.image
        titleLabel.text = config.title
        addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
    }

    @objc private func tap() {
        if let config = (row as? EHTitleRow)?.configuration {
            config.action?()
        }
    }

    private func setupArrowButton() {
        let arrow = UIImageView()
        arrow.image = UIImage(named: "navigationbar_btn_close")
        arrow.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(arrow)
        NSLayoutConstraint.activate([
            arrow.rightAnchor.constraint(equalTo: contentView.rightAnchor,
                                         constant: 20),
            arrow.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
            arrow.widthAnchor.constraint(equalToConstant: 12),
            arrow.heightAnchor.constraint(equalToConstant: 12)
            ])
        arrowImageView = arrow
    }

    private func setupTitleLabel() {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(label)
        NSLayoutConstraint.activate([
            label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20),
            label.leftAnchor.constraint(equalTo: thumbnail.rightAnchor, constant: 20)
            ])
        titleLabel = label
    }

    private func setupThumbnail() {
        let thumbN = UIImageView()
        thumbN.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(thumbN)
        NSLayoutConstraint.activate([
            thumbN.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 20),
            thumbN.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20),
            thumbN.widthAnchor.constraint(equalToConstant: 30),
            thumbN.heightAnchor.constraint(equalToConstant: 30)
            ])
        thumbnail = thumbN
    }
}

final class EHTitleRow: Row<EHTitleCell>, RowType {
    var configuration: EHTitleRowConfiguration?
}
